package com.sunagy.demand.backend.controller;

import com.sunagy.demand.backend.VO.ResultVO;
import com.sunagy.demand.backend.VO.request.DictTypeAddRequestVO;
import com.sunagy.demand.backend.VO.request.DictValueAddRequestVO;
import com.sunagy.demand.backend.VO.request.DictValueEditRequestVO;
import com.sunagy.demand.backend.VO.response.DictTypeResponseVO;
import com.sunagy.demand.backend.VO.response.DictValueResponseVO;
import com.sunagy.demand.backend.service.DictService;
import com.sunagy.demand.backend.utils.ResultVOUtil;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Slf4j
@RestController
@RequestMapping("/dict")
@Api(value = "数据字典", description = "数据字典相关接口")
public class DictController {

    @Autowired
    private DictService dictService;

    @ApiOperation(value = "新增一条数据", notes = "新增一条数据")
    @ApiResponses({
            @ApiResponse(code = 500, message = "接口异常")
    })
    @RequestMapping(value = "/param",method = RequestMethod.POST)
    public ResultVO<DictValueResponseVO> addDictValue(@RequestBody DictValueAddRequestVO dictValueAddRequestVO){
        DictValueResponseVO dictValueResponseVO = dictService.addDictValue(dictValueAddRequestVO);
        return ResultVOUtil.success(dictValueResponseVO);
    }

    @ApiOperation(value = "新增一条叶节点数据", notes = "新增一条叶节点数据")
    @ApiResponses({
            @ApiResponse(code = 500, message = "接口异常")
    })
    @RequestMapping(value = "/",method = RequestMethod.POST)
    public ResultVO<DictTypeResponseVO> addDictValue(@RequestBody DictTypeAddRequestVO dictTypeAddRequestVO){
        DictTypeResponseVO dictTypeResponseVO = dictService.addDictType(dictTypeAddRequestVO);
        return ResultVOUtil.success(dictTypeResponseVO);
    }

    @ApiOperation(value = "删除一条数据", notes = "删除一条数据")
    @ApiResponses({
            @ApiResponse(code = 500, message = "接口异常")
    })
    @RequestMapping(value = "/param/{id}",method = RequestMethod.DELETE)
    public ResultVO<Integer> deleteDictValue(@PathVariable Integer id){
        Integer delID = dictService.deleteDictValue(id);
        return ResultVOUtil.success(delID);
    }

    @ApiOperation(value = "删除一条叶节点数据", notes = "删除一条叶节点数据")
    @ApiResponses({
            @ApiResponse(code = 500, message = "接口异常")
    })
    @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
    public ResultVO<Integer> deleteDictType(@PathVariable Integer id){
        Integer delID = dictService.deleteDictType(id);
        return ResultVOUtil.success(delID);
    }

    @ApiOperation(value = "查询所有叶节点", notes = "列表返回叶节点不分页")
    @ApiResponses({
            @ApiResponse(code = 500, message = "接口异常")
    })
    @RequestMapping(value = "/",method = RequestMethod.GET)
    public ResultVO<List<DictTypeResponseVO>> getList(){
        List<DictTypeResponseVO> list =  dictService.getList();
        return ResultVOUtil.success(list);
    }

    @ApiOperation(value = "根据叶节点id查询数据", notes = "列表返回数据")
    @ApiResponses({
            @ApiResponse(code = 500, message = "接口异常")
    })
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public ResultVO<List<DictValueResponseVO>> getList(@PathVariable Integer id){
        List<DictValueResponseVO> list =  dictService.getDictValueList(id);
        return ResultVOUtil.success(list);
    }

    @ApiOperation(value = "编辑一条数据", notes = "根据id编辑一条数据")
    @ApiResponses({
            @ApiResponse(code = 500, message = "接口异常")
    })
    @RequestMapping(value = "/param/{id}",method = RequestMethod.PUT)
    public ResultVO<DictValueResponseVO> edit(@PathVariable Integer id,@RequestBody DictValueEditRequestVO dictValueEditRequestVO){
        DictValueResponseVO dictValueResponseVO =  dictService.editDictValue(id,dictValueEditRequestVO);
        return ResultVOUtil.success(dictValueResponseVO);
    }
}
